export default class CircleFly {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.lng = 34.6169472
    this.lat = 47.6458037
    this.initTime();
    this.initEntity();
    this.entity = null;
  }

  initTime() {
    this.start = Cesium.JulianDate.fromDate(new Date());
    // 设置总时长360秒
    this.stop = Cesium.JulianDate.addSeconds(this.start, 360, new Cesium.JulianDate());
    this.viewer.clock.startTime = this.start.clone();
    this.viewer.clock.stopTime = this.stop.clone();
    this.viewer.clock.currentTime = this.start.clone();
    this.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //Loop at the end
    this.viewer.clock.multiplier = 10;
    // this.beginAnimate(false);
    this.viewer.timeline.zoomTo(this.start, this.stop);
  }

  initEntity() {
    if (this.entity != null) return false;
    let cartesian = this.basicCesium.lnglatToWorld(this.lng, this.lat);
    this.entity = this.basicCesium.entityModel(new Cesium.CallbackProperty(() => {
      return cartesian
    }, false), {
      model: {
        uri: 'glb/aircraft.glb',
        minimumPixelSize: 100,
        maximumScale: 10000,
        scale: 1,
        incrementallyLoadTextures: false,
        asynchronous: false
      }
    });
    this.entity.isModel = true;
    this.viewer.trackedEntity = this.entity
    this.beginAnimate()
  }

  computedProperty(longitude, latitude, radius) {
    let property = new Cesium.SampledPositionProperty();
    for (var i = 0; i <= 360; i += 4) {
      // 根据360弧度，每4°一个拐点，随机生成90个点
      let radians = Cesium.Math.toRadians(i);
      let time = Cesium.JulianDate.addSeconds(this.start, i, new Cesium.JulianDate()); // 在总时长360秒中，设置中间点
      // 生成随机的坐标
      let position = Cesium.Cartesian3.fromDegrees(longitude + (radius * 1.5 * Math.cos(radians)), latitude + (radius * Math.sin(radians)), Cesium.Math.nextRandomNumber() * 500 + 50000);
      property.addSample(time, position);
    }
    return property;
  }

  beginAnimate(isTrue = true) {
    this.viewer.clock.shouldAnimate = isTrue;
    if (isTrue && this.entity != null && this.entity.position instanceof Cesium.CallbackProperty) {
      let radius = 1.2;
      let positions = this.computedProperty(this.lng, this.lat, radius);
      this.entity.availability = new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
        start: this.start,
        stop: this.stop
      })]);
      this.entity.position = positions;
      this.entity.orientation = new Cesium.VelocityOrientationProperty(positions);
      this.entity.path = {
        resolution: 1,
        material: new Cesium.PolylineGlowMaterialProperty({
          glowPower: 0.2,
          color: Cesium.Color.YELLOW
        }),
        width: 10
      };
      this.entity.position.setInterpolationOptions({
        // 直线
        interpolationDegree: 1,
        interpolationAlgorithm: Cesium.LinearApproximation
        // 拉格朗日插值
        // interpolationDegree: 5,
        // interpolationAlgorithm: Cesium.LagrangePolynomialApproximation

        //埃尔米特插值
        // interpolationDegree : 2,
        // interpolationAlgorithm : Cesium.HermitePolynomialApproximation
      });

    }
  }

  reset() {
    if (this.entity == null) return false;
    let cartesian = this.tools.lonlatToWorld(this.lng, this.lat, 500);
    this.entity.position = new Cesium.CallbackProperty(() => {
      return cartesian;
    }, false)
    this.entity.availability = null;
    this.entity.orientation = null;
  }
}